package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.ColleagueContactsModel;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.ResultSubscriber;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ColleagueContactsAdapter extends RecycleVAdapter<ColleagueContactsModel> implements
        StickyRecyclerHeadersAdapter<RecycleVHolder>,
        Comparator<ColleagueContactsModel>, View.OnClickListener, ResultSubscriber.OnResultListener {

    private static final int REQUEST_CODE = 0x01;

        private InviteLister   inviteLister;

    public void setInviteLister(InviteLister inviteLister) {
        this.inviteLister = inviteLister;
    }

    public ColleagueContactsAdapter(Context context, @NonNull List<ColleagueContactsModel> list) {
        super(context, list);
    }

    @Override
    public void updateAdapter(@NonNull List<ColleagueContactsModel> list) {
        mList.clear();
        Collections.sort(list, this);
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int compare(ColleagueContactsModel o1, ColleagueContactsModel o2) {
        if(o1 == null || o1.getRemark() == null || o2 == null || o2.getRemark() == null)
            return -1;
        return o1.getRemark().compareTo(o2.getRemark());
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_contacts;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        ImageLoadUtil.load(mContext,
                mList.get(position).getPortrait() + "?imageView2/2/w/100/interlace/1",
                holder.getImageView(R.id.item_contacts_iv_header),
                R.mipmap.ic_my_header
        );
        setTextView(holder.getTextView(R.id.item_contacts_tv_name), mList.get(position).getName());
        setTextView(holder.getTextView(R.id.item_contacts_tv_branch), mList.get(position).getPhone());

        if(mList.get(position).getName() != null
                && mList.get(position).getName().equals(mList.get(position).getPhone()))
            holder.get(R.id.item_contacts_tv_branch).setVisibility(View.GONE);
        else
            holder.get(R.id.item_contacts_tv_branch).setVisibility(View.VISIBLE);

        TextView inviteBn = holder.getTextView(R.id.item_contacts_btn_invite);
        inviteBn.setTag(position);
        inviteBn.setOnClickListener(this);
        if(mList.get(position).getIsInvite() == 0){
            inviteBn.setText("邀请");
            inviteBn.setBackgroundResource(R.drawable.my_rbg_blue_press);
            inviteBn.setTextColor(mContext.getResources().getColor(R.color.white));
        } else{
            inviteBn.setText("已邀请");
            inviteBn.setBackgroundResource(R.color.translate);
            inviteBn.setTextColor(mContext.getResources().getColor(R.color.gray_light));
        }
    }

    private void setTextView(TextView textView, String str){
        if(str == null || str.trim().length() == 0){
            textView.setVisibility(View.GONE);
        } else{
            textView.setVisibility(View.VISIBLE);
            textView.setText(str);
        }
    }

    @Override
    public long getHeaderId(int position) {
        return getData(position).getRemark().charAt(0);
    }

    @Override
    public RecycleVHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_remark_header, parent, false);
        return new RecycleVHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecycleVHolder holder, int position) {
        holder.setTextView(R.id.remark_header_tv, getData(position).getRemark().charAt(0)+"");
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if(mList.get(position).getIsInvite() == 1)
            return;
        if(inviteLister!=null)inviteLister.invite(mList.get(position));
//        JsonObject param = new JsonObject();
//        param.addProperty("token", HttpHelper.token);
//        param.addProperty("phone", mList.get(position).getPhone());
//        param.addProperty("id", mList.get(position).getPhone());
//        param.addProperty("type", mList.get(position).getPhone());
//        HttpHelper.getInstance(context).invitation(param, REQUEST_CODE, this);
//        ToasUtil.toast(context, "邀请信息发送中...");
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {}
    @Override
    public void onError(int requestType) {
        ToasUtil.toast(mContext, "抱歉，邀请信息发送失败，请稍后重试");
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(requestType != REQUEST_CODE)
            return;
        if(data.getErrcode() != 0)
            ToasUtil.toast(mContext, data.getMessage());
        else
            ToasUtil.toast(mContext, "你的邀请已经发出，请耐心等待对方的回复");
    }

    public interface   InviteLister{
        void invite(ColleagueContactsModel personMode);
    }
}
